
package com.scalar.chiptrack.operations.dataentry.actions;


import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.operations.dataentry.forms.SelectProductForm;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.dao.FabDAO;
import com.scalar.chiptrack.operations.process.dao.FabStage;
import com.scalar.chiptrack.operations.process.dao.FabSubStage;
import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;



public final class SelectProductAction extends ChipTrackAction {

    public SelectProductAction() {
    }

public ActionForward execute (ActionMapping mapping, 
                                  ActionForm form, 
                                  HttpServletRequest request, 
                                  HttpServletResponse response) throws Exception {


			ArrayList partNumberList = new ArrayList();

			ArrayList fabStageSubStageList = new ArrayList();
			SelectProductForm selectProductForm = (SelectProductForm) form;	

			

			String action = "";

			if(request.getParameter("action") != null){
				action = request.getParameter("action");
	        }

			partNumberList = ProductDao.getPartNumber("FAB");
			PartNumber partNumber = null;
			ArrayList partNumberSelectList = new ArrayList();

			for(int i=0;i<partNumberList.size();i++){
			partNumber = (PartNumber)partNumberList.get(i);
			partNumberSelectList.add(new org.apache.struts.util.LabelValueBean(partNumber.getPartNumber(),partNumber.getProcessStepId()+"$"+partNumber.getPartNumber()));
			}


			request.setAttribute("partNumberList",partNumberSelectList);
			request.setAttribute("selectProductForm",selectProductForm);

			if(action.equals("location") || action.equals("select")){

			request.setAttribute("locations","true");
			StringTokenizer st = new StringTokenizer(selectProductForm.getPartNumber(),"$");
			String strProcessStepId		= st.nextToken();
			String productName			= st.nextToken();
			ArrayList fabLocationList	= VendorDAO.getSelectedLocations(Integer.parseInt(strProcessStepId));
		
								
			if(fabLocationList == null)
			fabLocationList = new ArrayList(0);

			VendorLocation vendorLocation = null;
			ArrayList vendorLocationSelectList = new ArrayList();

			for(int i=0;i<fabLocationList.size();i++){
			vendorLocation = (VendorLocation)fabLocationList.get(i);
			vendorLocationSelectList.add(new org.apache.struts.util.LabelValueBean(vendorLocation.getLocationName(),vendorLocation.getLocationID()+"$"+vendorLocation.getLocationName()));
			}
			request.setAttribute("fabLocationList",vendorLocationSelectList);
			}

			
						
			if(action.equals("select")){


			FabDAO fabDao = new FabDAO();
			ArrayList fabStageList = new ArrayList();
			ArrayList fabSubStageList = new ArrayList();
			FabStage fabStage = null;
			FabSubStage fabSubStage = null;
			
			//System.out.println("partNumber "+selectProductForm.getPartNumber());
			//System.out.println("LocationName "+selectProductForm.getLocationName());
			
			if(selectProductForm.getPartNumber().equals("0") || selectProductForm.getLocationName().equals("0")){
	        request.setAttribute("ErrMsg","Product Name and Location are Manadatory");
			request.setAttribute("fabStageSubStageList",fabStageSubStageList);
			return (mapping.findForward("success")); 	
			}
					
			else{

		   StringTokenizer st = new StringTokenizer(selectProductForm.getPartNumber(),"$");
		   String strProcessStepId = st.nextToken();
		   String productName		 = st.nextToken();
		 	   	 
		   StringTokenizer st1 = new StringTokenizer(selectProductForm.getLocationName(),"$");
		   String strLocationId = st1.nextToken();
		   String locationName  = st1.nextToken(); 	
			
			request.setAttribute("processStepId",strProcessStepId);
			request.setAttribute("productName",productName);
			request.setAttribute("locationId",strLocationId);
			request.setAttribute("locationName",locationName);

			int processStepId = new Integer(strProcessStepId).intValue(); 
			int locationId = new Integer(strLocationId).intValue(); 
			fabStageSubStageList = FabDAO.getFabStages(processStepId,locationId);
			
			}
			    
	}
	request.setAttribute("fabStageSubStageList",fabStageSubStageList);
	return (mapping.findForward("success")); 
	}
	
}